<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Advertise extends FrontEndController {
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $aData = array('pageTitle' => 'Liên hệ quảng cáo');
        $theme = $this->config->item('front_theme');
        if($theme=='eshopper'){
            $aData['no-sidebar'] = true;
        }
        $this->renderView('static-pages/contact-advertise', $aData);
    }

    public function submitAdvert() {
        $aResult = array();
        $this->lang->load('error', 'vietnamese');
        $this->load->helper('recaptchalib');

        //check data
        $contact_ad = $this->input->post('contact_ad', true);
        $this->lang->load('form_validation', 'vietnamese');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('contact_ad[name]', 'lang:contact_ad_name', 'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('contact_ad[email]', 'lang:contact_ad_email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('contact_ad[anchor]', 'lang:contact_ad_anchor', 'trim|required|max_length[10000]xss_clean');
        $this->form_validation->set_rules('contact_ad[start_date]', 'lang:contact_ad_start_date', 'trim|required|max_length[10]|xss_clean');
        $this->form_validation->set_rules('contact_ad[end_date]', 'lang:contact_ad_end_date', 'trim|required|max_length[10]|xss_clean');
        $this->form_validation->set_rules('contact_ad[note]', 'lang:contact_ad_note', 'trim|required|max_length[10000]|xss_clean');
        $this->form_validation->set_error_delimiters('<li>', '</li>');

        //check captcha
        $errCaptcha = '';
        $gPrivateKey = getOption('google_recaptcha_private_key');
        $resp = recaptcha_check_answer($gPrivateKey, $_SERVER["REMOTE_ADDR"], $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'));
        if (!$resp->is_valid) {
            $errCaptcha = "<li>" . $this->lang->line('contact_ad_captcha') . " không đúng</li>";
        }

        if ($this->form_validation->run() == false || !$resp->is_valid) {
            $liValidate = validation_errors();
            if($errCaptcha!='') {
                $liValidate .= $errCaptcha;
            }
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('contact_ad_fail'),
                'text' => '<ul>' . $liValidate . '</ul>',
                );
            $this->renderJSON($aResult); return;
        }

        $aContactAd = array(
            'position'   => $contact_ad['position'],
            'anchor'     => $contact_ad['anchor'],
            'start_date' => $contact_ad['start_date'],
            'end_date'   => $contact_ad['end_date'],
            'note'       => $contact_ad['note'],
            'created_at' => get_current_date()
        );

        //handle upload
        $config['upload_path']   = './' .UPLOAD_FOLDER_URL . ADVERTISE_FOLDER_URL;
        $config['allowed_types'] = 'zip|rar';
        $config['max_size']      = '2048';
        $config['encrypt_name']  = true;
        $config['remove_spaces'] = true;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload()) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('contact_ad_fail'),
                'text' => '<ul><li>'.$this->upload->display_errors().'</li></ul>'
            );
            $this->renderJSON($aResult); return;
        }
        $data = $this->upload->data();
        $aContactAd['file_name'] = $data['file_name'];
        $aContactAd['opt_info'] = serialize(array(
            'name'  => $contact_ad['name'],
            'email' => $contact_ad['email']
        ));
        $this->load->model('repo/advertise_model', 'advertise_model');
        $result = $this->advertise_model->clientRegister($aContactAd);
        if ($result) {
            $aResult = array('code' => 1, 'text' => $this->lang->line('contact_ad_success'));
        } else {
            $aResult = array('code' => 0, 'title' => $this->lang->line('contact_ad_fail'));
        }
        $this->renderJSON($aResult);
    }
}
?>